package com.lxd.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import com.lxd.entity.po.File;
import com.lxd.entity.query.FileQuery;
import com.lxd.entity.vo.ActivityVO;
import com.lxd.entity.vo.ClubVO;
import com.lxd.mappers.FileMapper;
import com.lxd.redis.RedisUtil;
import com.lxd.utils.Constants;
import com.lxd.utils.CopyTools;
import org.springframework.stereotype.Service;

import com.lxd.entity.enums.PageSize;
import com.lxd.entity.query.ActivityQuery;
import com.lxd.entity.po.Activity;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.entity.query.SimplePage;
import com.lxd.mappers.ActivityMapper;
import com.lxd.service.ActivityService;
import com.lxd.utils.StringTools;


/**
 * 业务接口实现
 */
@Service("activityService")
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityMapper<Activity, ActivityQuery> activityMapper;
    @Resource
    private FileMapper<File, FileQuery> fileMapper;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<Activity> findListByParam(ActivityQuery param) {
        return this.activityMapper.selectList2(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(ActivityQuery param) {
        return this.activityMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<ActivityVO> findListByPage(ActivityQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<Activity> list = this.findListByParam(param);
        List<ActivityVO> vos = CopyTools.copyList(list, ActivityVO.class);
        Date curDate = new Date();
        for (ActivityVO vo : vos) {
            // 判断活动状态
            Date startTime = vo.getStartTime();
            Date endTime = vo.getEndTime();
            vo.setStatus(setStatus(startTime, endTime, curDate));
            vo.setUpdatedAt(curDate);
            // 获取图片路径
            //todo  枚举类
            vo.setImageUrl(getFilePath(vo.getId(), 0));

            // 修改存储方式
//            String key = "Activity:" + vo.getId();  // 使用单独的key存储每个活动
//            redisUtil.set(key, vo);
            // 或者使用 Hash 结构存储
            redisUtil.setHash("Activity", vo.getId().toString(), vo);
        }

        PaginationResultVO<ActivityVO> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), vos);
        return result;
    }

    /**
     * 获取关联内容的最新图片
     *
     * @param relatedId   关联ID
     * @param relatedType 关联类型（0：活动，1：新闻）
     * @return 图片URL
     */
    private String getFilePath(Long relatedId, Integer relatedType) {
        File file = fileMapper.selectLatestByRelatedId(relatedId, relatedType);
        return file == null ? "" : file.getFilePath();
    }

    // 设置活动状态
    private String setStatus(Date startTime, Date endTime, Date curDate) {
        if (curDate.after(endTime)) {
            return "已结束";
        } else if (curDate.before(startTime)) {
            return "未开始";
        } else {
            return "进行中";
        }
    }


    /**
     * 新增
     */
    @Override
    public Integer add(Activity bean) {
        Date date = new Date();
        bean.setUpdatedAt(date);
        bean.setCreatedAt(date);
        return this.activityMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<Activity> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.activityMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<Activity> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.activityMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(Activity bean, ActivityQuery param) {
        StringTools.checkParam(param);
        return this.activityMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(ActivityQuery param) {
        StringTools.checkParam(param);
        return this.activityMapper.deleteByParam(param);
    }

    /**
     * 根据Id获取对象
     */
    @Override
    public Activity getActivityById(Long id) {
        return this.activityMapper.selectById(id);
    }

    /**
     * 根据Id修改
     */
    @Override
    public Integer updateActivityById(Activity bean, Long id) {
        return this.activityMapper.updateById(bean, id);
    }

    /**
     * 根据Id删除
     */
    @Override
    public Integer deleteActivityById(Long id) {
        return this.activityMapper.deleteById(id);
    }

    @Override
    public Integer getTotalCount() {
        return activityMapper.selectCount(new ActivityQuery());
    }


}